package controller;

import java.io.IOException;
import java.util.List;
import java.util.Objects;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import common.ResultData;
import service.FlowerService;
import service.impl.FlowerServiceImpl;
import vo.FlowerVO;

@WebServlet("/getByPage")
public class FlowerServlet extends HttpServlet{

	FlowerService service = new FlowerServiceImpl();
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// 得到参数page
		String page = req.getParameter("page");
		// 如果page参数是null，返回1，否则非空转成整数
		int p = (Objects.isNull(page)) ? 1 : Integer.valueOf(page);
		
		List<FlowerVO> list = service.getFlowerByPage(p);
		
		ResultData result = new ResultData();
		if (list.isEmpty()) {
			result.setCode(10001);
			result.setMessage("查询失败");
		}else {
			result.setCode(200);
			result.setMessage("查询成功");
			result.setData(list);
		}
		// 设置给req对象
		req.setAttribute("result", result);
		// 转发到show_grid.jsp
		req.getRequestDispatcher("/show_grid.jsp").forward(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doGet(req, resp);
	}
}
